package net.xdclass.forum.dao;

import net.xdclass.forum.domain.Category;
import net.xdclass.forum.domain.Reply;
import net.xdclass.forum.domain.Topic;
import net.xdclass.forum.util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class TopicDao {

    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil
            .getDataSource());


    public int countTotalTopicByCid(int cId) {

        String sql = "select count(*) from topic where c_id=?";
        Long count = null;
        try {
            count = (long)queryRunner.query(sql, new ScalarHandler(), cId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count.intValue();
    }

    public List<Topic> findListByCid(int cId, int from, int pageSize) {
        String sql = "select * from topic where c_id=? order by update_time desc limit ?, ?";
        List<Topic> list = null;
        try {
            list = queryRunner.query(sql, new BeanListHandler<Topic>(Topic.class), cId, from, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    public Topic findById(int topicId) {
        String sql = "select * from topic where id=?";

        Topic topic = null;
        try {
            topic = queryRunner.query(sql, new BeanHandler<>(Topic.class),topicId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return topic;
    }

    /*
    查询该topicId下的reply条数
     */
    public int countTotalReplyByTopicId(int topicId) {

        String sql = "select count(*) from reply where topic_id=?";
        Long count = null;
        try {
            count = (long)queryRunner.query(sql, new ScalarHandler<>(),topicId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count.intValue();
    }

    /*
    分页查询topic下面的回复
     */
    public List<Reply> findReplyListByTopicId(int topicId, int from, int pageSize) {
        String sql = "select * from reply where topic_id=? order by floor asc limit ?, ?";
        List<Reply> list = null;
        try {
            list = queryRunner.query(sql, new BeanListHandler<Reply>(Reply.class), topicId, from, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    public int save(Topic topic) throws Exception {
        String sql = "insert into topic(c_id, title, content, pv, user_id, username, user_img, create_time, update_time, hot, `delete`) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] params = {
                topic.getC_id(),
                topic.getTitle(),
                topic.getContent(),
                topic.getPv(),
                topic.getUser_id(),
                topic.getUsername(),
                topic.getUser_img(),
                topic.getCreate_time(),
                topic.getUpdate_time(),
                topic.getHot(),
                topic.getDelete()
        };

        int i = 0;
        try {
            i = queryRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new Exception();
        }
        return i;
    }

    public int findLatestFloorByTopicId(int topicId) {
        String sql = "select floor from reply where topic_id = ? order by floor desc limit 1";

        int floor = 0;
        try {
            Object object = queryRunner.query(sql, new ScalarHandler<>(), topicId);
            if(object != null)
                floor = (Integer) object;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(floor < 0)
        {
            return 0;
        }
        else {
            return floor;
        }
    }

    public int replyByTopicId(Reply reply) throws Exception {

        String sql = "insert into reply(topic_id, floor, content, user_id, username, user_img, create_time, update_time, `delete`) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] params = {
                reply.getTopic_id(),
                reply.getFloor(),
                reply.getContent(),
                reply.getUser_id(),
                reply.getUsername(),
                reply.getUser_img(),
                reply.getCreate_time(),
                reply.getUpdate_time(),
                reply.getDelete()
        };
        int i = 0;
        try {
            i = queryRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new Exception();
        }
        return i;
    }
}
